package com.rzcode.coinex.chain.controller;

import com.rzcode.coinex.chain.config.JsonRpcClient;
import com.rzcode.coinex.kernel.common.response.R;
import com.rzcode.coinex.rpc.common.service.AccountService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("rpc/btc")
@RequiredArgsConstructor
public class WalletController {
    private final JsonRpcClient jsonRpcClient;
    //    @Autowired
//    private Coin coin;
    private final AccountService accountService;

    /**
     * 处理GET请求，用于获取区块链的最新高度。
     *
     * @return 包含区块链高度信息的MessageResult对象，或者在发生异常时返回错误信息。
     */
    @GetMapping("height")
    public R getHeight() {
        try {
            // 调用rpcClient的getBlockCount方法获取区块链的最新高度。
            int height = jsonRpcClient.getBlockCount();
            System.out.println(height);
            // 日志记录获取到的区块链高度。
            log.info("get block height: lasted height={}", height);
            // 创建一个MessageResult对象，表示成功，并将区块链高度减1后作为数据存储。
//            R result = R.ok(0, "success");
//            result.setData(height - 1);
            // 返回包含区块链高度的MessageResult对象。
//            return result;
            return R.ok(height);
        } catch (Exception e) {
            // 如果在执行过程中出现异常，打印堆栈跟踪信息。
//            e.printStackTrace();
//            // 创建并返回一个表示错误的MessageResult对象，包含错误代码和错误消息。
//            return MessageResult.error(500, "查询失败,error:" + e.getMessage());
            return R.fail(String.format("查询失败", e.getMessage()));
        }
    }

//    @GetMapping("address/{account}")
//    public MessageResult getNewAddress(@PathVariable String account) {
//        logger.info("create new address :" + account);
//        String address = rpcClient.getNewAddress(account);
//        accountService.saveOne(account, address);
//        MessageResult result = new MessageResult(0, "success");
//        result.setData(address);
//        return result;
//    }


//    @GetMapping("withdraw")
//    public MessageResult withdraw(String address, BigDecimal amount, BigDecimal fee) {
//        logger.info("withdraw:address={},amount={},fee={}", address, amount, fee);
//        try {
//            String txid = rpcClient.omniSend(coin.getWithdrawAddress(), address, amount);
//            MessageResult result = new MessageResult(0, "success");
//            result.setData(txid);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return MessageResult.error(500, "error:" + e.getMessage());
//        }
//    }

//    @GetMapping("transfer")
//    public MessageResult transfer(String address, BigDecimal amount) {
//        logger.info("transfer:address={},amount={},fee={}", address, amount);
//        try {
//            List<Account> accounts = accountService.findAll();
//            BigDecimal transferedAmt = BigDecimal.ZERO;
//            for (Account account : accounts) {
//                if (account.getAddress().equalsIgnoreCase(address)) continue;
//                BigDecimal btcFee = rpcClient.getAddressBalance(account.getAddress());
//                if (btcFee.compareTo(coin.getDefaultMinerFee()) < 0) {
//                    logger.info("地址{}矿工费不足，最小为{},当前为{}", account.getAddress(), coin.getDefaultMinerFee(), btcFee);
//                    continue;
//                }
//                BigDecimal availAmt = rpcClient.omniGetBalance(account.getAddress());
//
//                if (availAmt.compareTo(amount.subtract(transferedAmt)) > 0) {
//                    availAmt = amount.subtract(transferedAmt);
//                }
//                if (availAmt.compareTo(BigDecimal.ZERO) <= 0) {
//                    continue;
//                }
//                //String txid = rpcClient.omniSend(account.getAddress(),address,amount.toPlainString());
//                String txid = rpcClient.omniSend(account.getAddress(), address, availAmt);
//                if (txid != null) {
//                    System.out.println("fromAddress" + account.getAddress() + ",txid:" + txid);
//                    transferedAmt = transferedAmt.add(availAmt);
//                }
//                if (transferedAmt.compareTo(amount) >= 0) {
//                    break;
//                }
//            }
//            MessageResult result = new MessageResult(0, "success");
//            result.setData(transferedAmt);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return MessageResult.error(500, "error:" + e.getMessage());
//        }
//    }

//    @GetMapping("transfer-from-address")
//    public MessageResult transferFromAddress(String fromAddress, String address, BigDecimal amount, BigDecimal fee) {
//        logger.info("transfer:fromeAddress={},address={},amount={},fee={}", fromAddress, address, amount, fee);
//        try {
//            BigDecimal transferedAmt = BigDecimal.ZERO;
//            if (fromAddress.equalsIgnoreCase(address)) return MessageResult.error(500, "转入转出地址不能一样");
//            BigDecimal availAmt = rpcClient.omniGetBalance(fromAddress);
//            if (availAmt.compareTo(amount) > 0) {
//                availAmt = amount;
//            }
//            String txid = rpcClient.omniSend(fromAddress, address, amount);
//            if (txid != null) {
//                System.out.println("fromAddress" + fromAddress + ",txid:" + txid);
//                transferedAmt = transferedAmt.add(availAmt);
//            }
//            MessageResult result = new MessageResult(0, "success");
//            result.setData(transferedAmt);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return MessageResult.error(500, "error:" + e.getMessage());
//        }
//    }

//    @GetMapping("balance")
//    public MessageResult balance() {
//        BigDecimal amount = BigDecimal.ZERO;
//        try {
//            List<Account> accounts = accountService.findAll();
//            logger.info("get all balance:address count={}", accounts.size());
//            for (int i = 0; i < accounts.size(); i++) {
//                BigDecimal itAmount = rpcClient.omniGetBalance(accounts.get(i).getAddress());
//                amount = amount.add(itAmount);
//                logger.info("coin={}, address={}, balance={}", coin.getUnit(), accounts.get(i).getAddress(), itAmount);
//            }
//            MessageResult result = new MessageResult(0, "success");
//            result.setData(amount);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return MessageResult.error(500, "error:" + e.getMessage());
//        }
//    }


//    @GetMapping("balance/{address}")
//    public MessageResult balance(@PathVariable String address) {
//        try {
//            BigDecimal balance = rpcClient.omniGetBalance(address);
//            logger.info("get address balance:address={}, balance={}", address, balance);
//            MessageResult result = new MessageResult(0, "success");
//            result.setData(balance);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return MessageResult.error(500, "error:" + e.getMessage());
//        }
//    }

}
